package com.zj.leetcode.easy;

/**
 * @program: algorithm
 * @description: 557. 反转字符串中的单词 III
 * @author: Zhang Bo
 * @create: 2022-01-05 19:21
 **/
public class ReverseWordsInAStringIii {
    public static void main(String[] args) {
        Solution solution = new ReverseWordsInAStringIii().new Solution();
        System.out.println(solution.reverseWords("Let's take LeetCode contest"));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String reverseWords(String s) {
            String[] split = s.split(" ");
            String ret = "";
            for (int i = 0; i < split.length; i++) {
                char[] chars = split[i].toCharArray();
                if (i == 0)
                    ret = ret + asString(chars);
                else
                    ret = ret + " " + asString(chars);
            }
            return ret;
        }

        private String asString(char[] chars) {

            int i = 0;
            while (i <= (chars.length - 1) / 2) {
                char temp = chars[i];
                chars[i] = chars[(chars.length - 1) - i];
                chars[(chars.length - 1) - i] = temp;
                i++;
            }

            return String.valueOf(chars);
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
